import axios from 'axios'

const baseUrl = '/api'

class HttpRequest {
  constructor(baseUrl) {
    this.baseUrl = baseUrl

    this.instance = axios.create()

    this.interceptors(this.instance)
  }

  getInsideConfig() {
    const config = {
      baseUrl: this.baseUrl,
      headers: {} 
    }
    return config
  }


  interceptors(instance) {
    // Request interceptor
    instance.interceptors.request.use(function (config) {
      return config
    }, function (error) {
      return Promise.reject(error)
    })

    // Response interceptor
    instance.interceptors.response.use(function (response) {
      return response
    }, function (error) {
      console.log(error, 'error')
      return Promise.reject(error)
    })
  }


  request(options) {
    options = { ...this.getInsideConfig(), ...options }
    return this.instance(options) 
  }
}


const httpRequestInstance = new HttpRequest(baseUrl)


export default httpRequestInstance
